//
//  WBTextView.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/5/21.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class WBTextView: UITextView {

    fileprivate lazy var placeholderLabel = UILabel()
    
    override func awakeFromNib() {

        setupUI()
        
        //注册通知
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(textViewDidChangeNotification),
            name: NSNotification.Name.UITextViewTextDidChange,
            object: self)
    }
    
    // MARK: -- 视图销毁的时候,调用的方法
    deinit {
        
        //注销通知
        NotificationCenter.default.removeObserver(self)
    }
}

//为了体验代理一对一的关系
//extension WBTextView: UITextViewDelegate{
//    
//    func textViewDidChange(_ textView: UITextView) {
//        
//        print("哈哈")
//    }
//}

// MARK: -- 设置UI
fileprivate extension WBTextView{
    
    func setupUI() {
        
        //设置占位标签的属性
        placeholderLabel.font = font
        placeholderLabel.text = "分享新鲜事..."
        placeholderLabel.textColor = UIColor.darkGray
        placeholderLabel.sizeToFit()
        placeholderLabel.frame.origin = CGPoint(x: 5, y: 8)
        
        addSubview(placeholderLabel)
        
        //为了体验代理一对一的关系
//        self.delegate = self
    }
    
    @objc func textViewDidChangeNotification(){
    
        placeholderLabel.isHidden = hasText
    }
}
